package com.guguskins.back.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class SendMailUtil {

    private static JavaMailSender javaMailSender;

    private static String email;

    @Autowired
    private JavaMailSender javaMailSenderTemplate;

    @Value("${spring.mail.username}")
    private String emailTemplate;

    @PostConstruct
    public void init() {
        javaMailSender = this.javaMailSenderTemplate;
        email = this.emailTemplate;
    }

    /**
     * 最简单的邮件发送
     *
     * @param from    发送方
     * @param to      接收方
     * @param cc      第三方(非必填)
     * @param subject 标题
     * @param content 内容
     */
    public static void sendSimpleMail(String from, String to, String cc, String subject, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
//        simpleMailMessage.setCc(cc);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    public static void sendSimpleMail(String to, String cc, String subject, String content) {
        sendSimpleMail(email, to, cc, subject, content);
    }

    public static void sendSimpleMail(String to, String subject, String content) {
        sendSimpleMail(email, to, null, subject, content);
    }


    /**
     * 代附件的邮件
     *
     * @param from    发送方
     * @param to      接收方
     * @param cc      第三方(非必填)
     * @param subject 标题
     * @param content 内容
     * @param file    附件
     */
    public void sendAttachFileMail(String from, String to, String cc, String subject, String content, File file) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
//            mimeMessageHelper.setCc(cc);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content);
            mimeMessageHelper.addAttachment(file.getName(), file);
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendAttachFileMail(String to, String cc, String subject, String content, File file) {
        this.sendAttachFileMail(email, to, cc, subject, content, file);
    }

    /**
     * 带图片的邮件
     *
     * @param from    发送方
     * @param to      接收方
     * @param cc      第三方(非必填)
     * @param subject 标题
     * @param content 内容
     * @param srePath 图片地址
     * @param resIds  图片标题
     */
    public void sendMailWithImg(String from, String to, String cc, String subject, String content, String[] srePath, String[] resIds) {
        if (srePath.length != resIds.length) {
            System.out.println("发送失败");
            return;
        }
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            for (int i = 0; i < srePath.length; i++) {
                FileSystemResource res = new FileSystemResource(new File(srePath[i]));
                mimeMessageHelper.addInline(resIds[i], res);
            }
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public void sendMailWithImg(String to, String cc, String subject, String content, String[] srePath, String[] resIds) {
        this.sendMailWithImg(email, to, cc, subject, content, srePath, resIds);
    }

    /**
     * 使用thymeleaf模板
     *
     * @param from    发送方
     * @param to      接收方
     * @param cc      第三方(非必填)
     * @param subject 标题
     * @param context 内容
     */
    public static void sendHtmlMail(String from, String to, String cc, String subject, String context) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
//            mimeMessageHelper.setCc(cc);
            mimeMessageHelper.setText(context);
            mimeMessageHelper.setSubject(subject);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void sendHtmlMail(String to, String cc, String subject, String context) {
        sendHtmlMail(email, to, cc, subject, context);
    }

    /**
     * 验证邮箱
     */
    public static boolean checkEmail(String email) {
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }
}